package com.ly.wxstore.service;

import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.wxstore.comm.MyPage;
import com.ly.wxstore.entity.PrizeUserinfo;
import com.ly.wxstore.repository.PrizeUserinfoDao;

/**
 * 
 * 
 * @author Peter
 */
// Spring Service Bean的标识.
@Component
public class PrizeUserinfoService {

	private static Logger logger = LoggerFactory.getLogger(PrizeUserinfoService.class);

	@Autowired
	private PrizeUserinfoDao prizeUserinfoDao;

	public int getReceived(String uid){
		int received = 0;
		List<PrizeUserinfo> list = prizeUserinfoDao.getByUid(uid);
		if(list == null || list.size()==0){
			return received;
		}else{
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				PrizeUserinfo prizeUserinfo = (PrizeUserinfo) iterator.next();
				if(prizeUserinfo.getReceived()==1){
					received = 1;
				}
				
			}
			return received;
		}
	}
	
	public PrizeUserinfo getById(Long id) {
		return prizeUserinfoDao.getById(id);
	}

	public List<PrizeUserinfo> getAll() {
		return prizeUserinfoDao.getAll();
	}

	/**
	 * 分页查询
	 * 
	 * @param overtime
	 * @param pageStart
	 * @param pageSize
	 * @return
	 */
	public MyPage<PrizeUserinfo> searchPage(PrizeUserinfo prizeUserinfo, int currentPage, int pageSize) {
		MyPage<PrizeUserinfo> myPage = new MyPage<PrizeUserinfo>();

		Long count = prizeUserinfoDao.searchCount(prizeUserinfo);

		int pageStart = (currentPage - 1) < 0 ? 0 : (currentPage - 1) * pageSize;
		List<PrizeUserinfo> list = prizeUserinfoDao.searchPage(prizeUserinfo, pageStart, pageSize);

		myPage.setCount(count);
		myPage.setContent(list);

		return myPage;
	}

	public void save(PrizeUserinfo prizeUserinfo) {
		prizeUserinfoDao.save(prizeUserinfo);
	}

	public void update(PrizeUserinfo prizeUserinfo) {
		prizeUserinfoDao.update(prizeUserinfo);
	}

	/**
	 * 软删除
	 */
	public void delete(Long id) {
		prizeUserinfoDao.delete(id);
	}

	public List<PrizeUserinfo> getByUid(String uid) {
		return prizeUserinfoDao.getByUid(uid);
	}
}
